<template>
    <div class="home-detail-layout">
        <div class="head-part">
            <img v-if="logoImageUrl==='' " scr="" style="visibility:hidden ">
            <img v-else style=" width: 0.5rem; height: 0.5rem;float:left; margin: 13px;object-fit: cover;"
                 :src="logoImageUrl">
            <p class="head-bank-name">你好，{{partnerName}}用户！</p>
            <div class="message-background" @click="gotomessage">
                <img src="../../assets/xiaoxi.png" class="message-img">
                <div class="yuanbg">
                    <yd-badge bgcolor="#f38209">{{systemNum}}</yd-badge>

                </div>
            </div>

            <yd-slider autoplay="2000" style="height: 2.5rem">
                <yd-slider-item v-for="(item,index) in bannerList">
                    <div>
                        <a :href="bannerList[index].url" style="width: 100%;height: 100%">
                            <img :src="bannerList[index].imageUrl" style="width: 100%;height: 2.5rem;object-fit: fill;">
                        </a>
                    </div>
                </yd-slider-item>
            </yd-slider>
        </div>

        <div class="home-detail-menu-part" v-for="(item,index) in module">
            <p class="home-group-name-part">{{item.groupName}}</p>

            <div class="home-group-module-part" v-for="(item,index) in item.modules">

                <div v-if="item.size ==='8'"
                     class="home-eight-module-part"
                     @click="gotoNext(item)"
                     v-bind:style="{backgroundImage: 'url(\''+item.doubledImageUrl+'\')'}">
                    <p class="module-name-part">{{item.moduleName}}</p>
                    <p class="module-comment-part">{{item.comment}}</p>
                </div>

                <div v-if="item.size ==='4'"
                     class="home-four-module-part"
                     @click="gotoNext(item)"
                     v-bind:style="{backgroundImage: 'url(\''+item.imageUrl+'\')'}">
                    <p class="module-name-part">{{item.moduleName}}</p>
                    <p class="module-comment-part">{{item.comment}}</p>
                </div>
            </div>
        </div>
        <div style="width: 100%;height: 1.4rem;background: #ffffff"></div>
    </div>
</template>

<script>
    import {indexInfo,getNewMessageCount,machineUuidDetails} from "../../pcof/post";
    import {userInfoData,isEditRepairOrder,repairOrderSavedInstance,repairOrderData,instObject,providerObject} from '../../globalVariables'
    export default {
        name: "homeDetail",
        data() {
            return {
                partnerName: '',
                logoImageUrl: '',
                bannerList: [],
                module: [],
                systemNum: '',
                partnerUuid: '',
            };
        },
        async created() {
            console.log('wxjs_is_wkwebview');
            console.log(window.wxjs_is_wkwebview);
            if (JSON.parse(localStorage.getItem('insthistorysearch')) === null || JSON.parse(localStorage.getItem('insthistorysearch')) === '') {
                localStorage.setItem('insthistorysearch', JSON.stringify
                ([]));
            }

            this.partnerUuid = JSON.parse(localStorage.getItem(userInfoData)).partnerUuid;
            // console.log(localStorage.getItem(userInfoData));
            // 客户编号 partnerUuid
            //银行编号 bankUuid
            var bankUuid = JSON.parse(localStorage.getItem(userInfoData)).partnerUuid;
            var mobile = JSON.parse(localStorage.getItem(userInfoData)).mobile;
            var response = await indexInfo(mobile, bankUuid);
            this.$dialog.loading.open('加载中...');
            if (response.data.code === 200) {
                this.$dialog.loading.close();
                this.partnerName = response.data.data.logo.partnerName;
                if (response.data.data.logo.logoImageUrl === null || response.data.data.logo.logoImageUrl === '') {
                    this.logoImageUrl = '';

                } else {
                    this.logoImageUrl = response.data.data.logo.logoImageUrl;

                }
                this.bannerList = response.data.data.bannerList;
                this.module = response.data.data.module;

            } else {
                this.$dialog.loading.close();
                this.$dialog.toast({
                    mes: response.data.msg,
                    timeout: 1500
                });
            }
            var response2 = await getNewMessageCount(mobile);
            console.log(response2);
            if (response2.data.code === 200) {
                this.systemNum = response2.data.data.count;
            } else {
                this.$dialog.loading.close();
                this.$dialog.toast({
                    mes: response.data.msg,
                    timeout: 1500
                });
            }


        },
        methods: {
            gotomessage()//进入消息列表
            {
                this.$router.push({name: 'messageList'});

            },
            async gotoNext(item) {
                console.log(item);

                // 设备报修 ORDER_APPLY_CODE
                // 凭证请领 VOUCHER_APPLY_CODE
                // 办公用品 OFFICE_SUPPLIES_CODE
                // 身份验证 QR_IDENTIFY_CODE
                // 设备查询 QUERY_EQUIP_CODE
                // 录入维修单 LOG_WARNING_ORDER

                if (item.moduleEventCode === 'ORDER_APPLY_CODE') {
                    // 设备报修 ORDER_APPLY_CODE

                    this.$router.push({name: 'repairEquipment'});

                } else if (item.moduleEventCode === 'VOUCHER_APPLY_CODE') {
                    // 凭证请领 VOUCHER_APPLY_CODE
                    if (JSON.parse(localStorage.getItem('voucherhistorysearch')) === null || JSON.parse(localStorage.getItem('voucherhistorysearch')) === '') {
                        localStorage.setItem('voucherhistorysearch', JSON.stringify
                        ([]));
                    }

                    if (JSON.parse(localStorage.getItem('voucher')) === null || JSON.parse(localStorage.getItem('voucher')) === '') {
                        localStorage.setItem('voucher', JSON.stringify
                        ([]));
                    } else {

                    }

                    this.$router.push({name: 'voucherList'});

                } else if (item.moduleEventCode === 'OFFICE_SUPPLIES_CODE') {
                    // 办公用品 OFFICE_SUPPLIES_CODE

                    if (JSON.parse(localStorage.getItem('officehistorysearch')) === null || JSON.parse(localStorage.getItem('officehistorysearch')) === '') {
                        localStorage.setItem('officehistorysearch', JSON.stringify
                        ([]));
                    }

                    if (JSON.parse(localStorage.getItem('officedata')) === null || JSON.parse(localStorage.getItem('officedata')) === '') {
                        localStorage.setItem('officedata', JSON.stringify
                        ([]));
                    } else {

                    }
                    this.$router.push({name: 'officeContainer'});


                } else if (item.moduleEventCode === 'QR_IDENTIFY_CODE') {
                    // 身份验证 QR_IDENTIFY_CODE
                    // this.$router.push({name: 'identityAuthentication'});
                    let that = this;
                    // this.$router.push({name: 'identityAuthentication',params: {identity: 'IDENTITY30e8155a-9766-400a-8121-f17a9577f089'}});
                    wx.ready(function () {
                        wx.scanQRCode({
                            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                            scanType: ["qrCode"], // "qrCode","barCode"可以指定扫二维码还是一维码，默认二者都有
                            success: function (res) {
                                var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                                // that.$dialog.confirm({
                                //     title: '选填标题',
                                //     mes: result,
                                //     opts: () => {
                                //         this.$dialog.toast({mes: '你点了确定', timeout: 1000});
                                //     }
                                // });
                                if (result.indexOf("IDENTITY") !== -1) {
                                    that.$router.push({name: 'identityAuthentication', params: {identity: result}});
                                } else {
                                    that.$dialog.toast({
                                        mes: "二维码不属于点点维,请验证后重试",
                                        timeout: 1500
                                    });
                                }
                            }
                        });
                    });


                } else if (item.moduleEventCode === 'QUERY_EQUIP_CODE') {
                    // 设备查询 QUERY_EQUIP_CODE

                    let that = this;
                    wx.ready(function () {
                        wx.scanQRCode({
                            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                            scanType: ["qrCode"], // "qrCode","barCode"可以指定扫二维码还是一维码，默认二者都有
                            success: function (res) {
                                var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                                if (result.indexOf('LittleBitDimension') !== -1) {
                                    var obj = JSON.parse(result);
                                    that.machineUuidDetailsFunc(obj.uuid, obj.name);
                                    // that.$router.push({name: 'equipmentDetailrecord', params: {equipmentuuid: obj.uuid,type:'2'}});
                                } else {
                                    that.$dialog.toast({
                                        mes: "二维码不属于点点维,请验证后重试",
                                        timeout: 1500
                                    });
                                }
                            }
                        });
                    });
                    //   this.$router.push({name: 'equipmentDetailrecord', params: {equipmentuuid:  'a179eaf22a0a4ed5ac97b8b732c08a33',type:'2'}});


                } else if (item.moduleEventCode === 'LOG_WARNING_ORDER') {
                    // 录入维修单 LOG_WARNING_ORDER
                    //清理录入维修单相关的缓存
                    localStorage.removeItem(repairOrderSavedInstance);
                    localStorage.removeItem(repairOrderData);
                    localStorage.removeItem(isEditRepairOrder);
                    localStorage.removeItem(instObject);
                    localStorage.removeItem(providerObject);
                    localStorage.setItem(isEditRepairOrder, '1');
                    this.$router.push({name: 'repairOrder'});
                }


            },
            async machineUuidDetailsFunc(uuid, name) {
                var response = await machineUuidDetails(
                    JSON.parse(localStorage.getItem(userInfoData)).mobile, uuid);
                if (response.data.code === 200) {
                    var equipDetailBean = response.data.data;
                    if (equipDetailBean.status === '1') {
                        this.$dialog.toast({
                            mes: '设备已删除',
                            timeout: 1500
                        });
                    } else if (equipDetailBean.status === '0') {
                        if (this.partnerUuid === equipDetailBean.partnerUuid) {
                            if (equipDetailBean.isBlank === '0') {
                                this.$dialog.toast({
                                    mes: '设备为空',
                                    timeout: 1500
                                });
                            } else if (equipDetailBean.isBlank === '1') {
                                // type//0 是不需要关联 从详情页过来 1 是需要关联 详情页过来 2 是可以报修从设备查询页过来
                                this.$router.push({
                                    name: 'equipmentDetailrecord',
                                    params: {equipmentuuid: uuid, type: '2'}
                                });
                            }
                        } else {
                            this.$dialog.toast({
                                mes: '不属于本机构的设备',
                                timeout: 1500
                            });
                        }

                    } else if (equipDetailBean.status === '2') {
                        this.$dialog.toast({
                            mes: '设备已报废',
                            timeout: 1500
                        });
                    }
                } else {
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            }
        },
    }
</script>

<style lang="scss">
    .home-detail-layout {
        .head-part {
            background-color: white;

            .head-bank-name {
                text-align: left;
                color: #282828;
                float: left;
                margin: 15px 0 10px 10px;
                /*font-size: 0.25rem;*/
                font-size: 1.4em;

            }

            .message-background {
                /*position: absolute;*/
                float: right;
                padding: 0.2rem 0.2rem 0 0;
                height: 1rem;
                right: 0rem;
                top: 0.2rem;
                width: 1.2rem;

                .message-img {
                    /*position: absolute;*/
                    width: 0.5rem;
                    height: 0.5rem;
                    margin-top: 0.2rem;
                }

                .yuanbg {
                    /*float: right;*/
                    /*position: absolute;*/
                    /*left: 0.35rem;*/
                    height: 0.4rem;
                    margin-top: -0.8rem;
                    margin-left: 0.5rem;

                    width: 0.4rem;

                }

            }

            .yd-slider {
                height: 40%;
                width: 100%;
                .yd-slider-item {
                    padding-left: 0.20rem;
                    padding-right: 0.20rem;
                }
            }

        }

        .home-detail-menu-part {
            background: #ffffff;
            display: flex;
            flex-wrap: wrap;
            width: 100%;

            .home-group-name-part {
                text-align: left;
                padding: 0.20rem 0.20rem 0.20rem 0.20rem;
                color: #808080;
                width: 100%;
                font-size: 1.4em;

            }

            .home-group-module-part {
                display: flex;
                flex-wrap: wrap;
                flex-direction: row;
                padding-top: 0.1rem;
                padding-left: 0.2rem;

                .home-eight-module-part {
                    height: 2.4rem;
                    width: 7.1rem;
                    border-radius: 0.2rem;
                    align-items: center;
                    background-repeat: no-repeat;
                    -webkit-background-size: cover;
                    -moz-background-size: cover;
                    -o-background-size: cover;
                    background-size: cover;

                    .module-name-part {
                        text-align: left;
                        padding-top: 0.6rem;
                        padding-left: 0.6rem;
                        color: white;
                        font-size: 1.4em;
                    }

                    .module-comment-part {
                        text-align: left;
                        color: white;
                        padding-left: 0.6rem;
                        padding-top: 0.1rem;
                        font-size: 1.1em;

                    }
                }

                .home-four-module-part {
                    height: 3.0rem;
                    width: 3.4rem;
                    align-items: center;
                    border-radius: 0.2rem;
                    background-repeat: no-repeat;
                    -webkit-background-size: cover;
                    -moz-background-size: cover;
                    -o-background-size: cover;
                    background-size: cover;

                    .module-name-part {
                        text-align: left;
                        color: white;
                        font-size: 1.4em;
                        padding-left: 0.4rem;
                        padding-top: 0.6rem;
                    }

                    .module-comment-part {
                        text-align: left;
                        color: white;
                        padding-left: 0.4rem;
                        padding-top: 0.1rem;
                        font-size: 1.1em;

                    }
                }

            }

        }
    }
</style>